<template>
  <el-dialog :title="title" :visible.sync="isShow" :show-close="false" :close-on-click-modal="false" :close-on-press-escape="false" @open="openEvent">
    <el-form :model="form">
      <el-row>
        <el-col :span="12">
          <el-form-item label="公司名称" :label-width="formLabelWidth">
            <el-input v-model="orgName " autocomplete="off" :disabled="true"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="岗位名称" :label-width="formLabelWidth">
            <el-input v-model="form.postName" autocomplete="off"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="状态" :label-width="formLabelWidth">
            <el-radio-group v-model="form.status">
              <el-radio label="0">正常</el-radio>
              <el-radio label="1">停用</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="排序" :label-width="formLabelWidth">
            <el-input v-model="form.sort" autocomplete="off"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="备注" :label-width="formLabelWidth">
            <el-input v-model="form.remark" autocomplete="off" type="textarea" :rows="2" placeholder="请输入备注"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="closeDialog">取 消</el-button>
      <el-button type="primary" @click="operatePost">确 定</el-button>
    </div>
  </el-dialog>
</template>
        
<script>
import { addPost, updatePost } from '@/api/post'

export default {
  name: 'OrgInfoComp',
  data () {
    return {
      form: {
        orgId: '',
        postName: '',
        sort: 0,
        status: "0",
        remark: ''
      },
      formLabelWidth: '120px',
      title: '新增岗位',
      orgName: undefined
    }
  },
  props: {
    isShow: {
      type: Boolean,
      required: true
    },
    orgItem: {
      type: Object,
      require: true
    },
    item: {
      type: Object,
      require: false
    }
  },
  methods: {
    openEvent () {
      this.orgName = this.orgItem.companyName === undefined ? this.orgItem.orgName : this.orgItem.companyName
      if (this.item === undefined) {
        this.form.orgId = this.orgItem === undefined ? "" : this.orgItem.id
        this.form.postName = ""
        this.form.remark = ""
        this.form.status = "0"
        this.form.sort = 0
        this.title = '新增岗位'
      } else {
        this.title = '编辑岗位'
        this.form = { ...this.item }
      }
    },
    closeDialog () {
      this.$emit("changeDialogStatus", false);
    },
    result (data) {
      this.$emit("changeDialogStatus", "success");
      if (data.status === 200) {
        this.$message.success("操作成功");
      } else {
        this.$message.error("操作失败，请重试");
      }
    },
    operatePost () {
      const param = { ...this.form }
      if (this.item === undefined) {
        addPost(param).then((res) => {
          this.result(res)
        });
      } else {
        updatePost(param).then((res) => {
          this.result(res)
        });
      }
    }
  },
  watch: {
    // // 监听company数据的变化
    // company (newValue, oldValue) {
    //   console.log(`company changed from ${oldValue} to ${newValue}`);
    //   // 在这里可以执行更多的逻辑
    //   if (newValue !== null || newValue !== undefined) {
    //     this.getOrgList()
    //   }
    // }
  }
}
</script>
        
<style scoped>
</style>
        